package com.xingda.packing.dmds.minitor.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.dmds.annotation.LimitRequest;
import com.xingda.packing.dmds.common.BaseController;
import com.xingda.packing.dmds.common.OperationConstant;
import com.xingda.packing.dmds.common.R;
import com.xingda.packing.dmds.annotation.OpLog;
import com.xingda.packing.dmds.domain.PackProductionLine;
import com.xingda.packing.dmds.minitor.dto.PkBoxLocationRealtionDto;
import com.xingda.packing.dmds.minitor.entity.PkAction;
import com.xingda.packing.dmds.minitor.entity.PkBoxLocation;
import com.xingda.packing.dmds.minitor.mapper.PkActionMapper;
import com.xingda.packing.dmds.minitor.service.IPkBoxLocationRealtionService;
import com.xingda.packing.dmds.minitor.service.IPkBoxLocationService;
import com.xingda.packing.dmds.plc.service.PlcDirectiveService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@RestController("pkBoxLocationRealtionDMDSController")
@RequestMapping("/api/boxLocationRealtion")
public class PkBoxLocationRealtionController extends BaseController {

    @Resource
    private IPkBoxLocationRealtionService pkBoxLocationRealtionService;
    @Resource
    private PlcDirectiveService plcDirectiveService;
    @Resource
    private IPkBoxLocationService pkBoxLocationService;
    @Resource
    PackProductionLine packProductionLine;
    @Resource
    PkActionMapper pkActionMapper;

    @PostMapping("/reset")
    @LimitRequest(count = 1)
    @OpLog(username = "#realtion.userDto.username",
            corp = "#realtion.userDto.corp",
            veri = "#realtion.userDto.veri",
            opItem = OperationConstant.RESET_TASK,
            param = "#realtion.boxLocationId")
    public R deleteBoxLocationRealtion(@RequestBody PkBoxLocationRealtionDto realtion) {
        if (packProductionLine.getEnv().startsWith("xd-prod")) {
            PkBoxLocation pkBoxLocation = pkBoxLocationService.getById(realtion.getBoxLocationId());
            if (plcDirectiveService.isTrue(pkBoxLocation.getPalletComing())) {
                return R.error("当前托盘未铲出 重置失败");
            }
        }
        LambdaQueryWrapper<PkAction> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(PkAction::getActionType, 2, 4);
        lambdaQueryWrapper.eq(PkAction::getFinishState, 1);
        int i = realtion.getBoxLocationId() < 11 ? 1 : 2;
        lambdaQueryWrapper.eq(PkAction::getArea, i);
        lambdaQueryWrapper.eq(PkAction::getToLocation, realtion.getBoxLocationId());
        PkAction pkAction = pkActionMapper.selectOne(lambdaQueryWrapper);
        if (Objects.nonNull(pkAction)) {
            return R.error("当前装箱位存在未完成的任务,重置失败!请去后台管理页面:[操作管理-动作操作],确认完毕再进行重置操作");
        }
        boolean flag = this.pkBoxLocationRealtionService.deleteBoxLocationRealtion(realtion.getBoxLocationId());
        return R.ok().put("data", flag);
    }
}

